package com.zs.propertymanage.controller;

import com.zs.propertymanage.dataObj.DTO;
import com.zs.propertymanage.dataObj.R;
import com.zs.propertymanage.dataObj.entity.Log;
import com.zs.propertymanage.dataObj.param.LogQueryParams;
import com.zs.propertymanage.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LogMapper logMapper;

    @PostMapping("")
    public R getLog(@RequestBody LogQueryParams params) {
        Integer currentPage = params.getCurrentPage();
        Integer pageSize = params.getPageSize();
        LocalDateTime[] dateRange = params.getDateRange();

        int start = (currentPage - 1) * pageSize;
        LocalDateTime startDate = dateRange[0].plusHours(8);
        LocalDateTime endDate = dateRange[1].plusHours(8);

        List<Log> logList = logMapper.getLog(start, pageSize, startDate, endDate);
        Integer total = logMapper.getLogCount(startDate, endDate);
        return logList.size() > 0 ? R.ok("查询日志信息成功", new DTO<>(logList, total)) : R.error("没有日志信息");
    }

}
